/**
 * 用synchronized来解决买票问题
 */
public class SellTicket01 {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(()->{
            for (int i = 0; i < 80; i++) {
                ticket.sale();
            }
        },"小明").start();
        new Thread(()->{
            for (int i = 0; i < 80; i++) {
                ticket.sale();
            }
        },"大刘").start();
        new Thread(()->{
            for (int i = 0; i < 80; i++) {
                ticket.sale();
            }
        },"胖子").start();
    }
}

class Ticket{
    private int count = 250;
    public synchronized void sale(){
        if (count>0){
            System.out.println( Thread.currentThread().getName() + "买了第" + (count--) + "张票，剩余" +  count+ "张票。");
        }
    }
}